package com.corechina.apex_osswords.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.corechina.apex_osswords.common.Response;
import com.corechina.apex_osswords.common.ShiroUtils;
import com.corechina.apex_osswords.domain.dao.CoreProjectTypeMapper;
import com.corechina.apex_osswords.domain.entity.CoreProjectType;
import com.corechina.apex_osswords.service.ICoreProjectTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 项目分类表 服务实现类
 * </p>
 *
 * @author cc
 * @since 2022-03-11
 */
@Service
public class CoreProjectTypeServiceImpl extends ServiceImpl<CoreProjectTypeMapper, CoreProjectType> implements ICoreProjectTypeService {

    @Autowired
    private CoreProjectTypeMapper coreProjectTypeMapper;

    @Override
    public Response add(CoreProjectType coreProjectType) {
        if (coreProjectTypeMapper.selectCount(new QueryWrapper<CoreProjectType>().eq("type_name", coreProjectType.getTypeName()).eq("enterprise_id", ShiroUtils.getEnterpriseId())) > 0) {
            return Response.error("该分类已存在，请勿重复添加");
        }
        coreProjectType.setEnterpriseId(ShiroUtils.getEnterpriseId());
        coreProjectTypeMapper.insert(coreProjectType);
        return Response.ok();
    }

    @Override
    public Response update(CoreProjectType coreProjectType) {
        CoreProjectType coreProjectType1 = coreProjectTypeMapper.selectOne(new QueryWrapper<CoreProjectType>().eq("type_name", coreProjectType.getTypeName()).eq("enterprise_id", ShiroUtils.getEnterpriseId()));
        if (coreProjectType1 != null && !coreProjectType1.getId().equals(coreProjectType.getId())) {
            return Response.error("该分类已存在，请勿重复添加");
        }
        coreProjectTypeMapper.updateById(coreProjectType);
        return Response.ok();
    }

}
